package com.kuiandjie.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kuiandjie.entity.Idea;
import com.kuiandjie.entity.IdeaComment;
import com.kuiandjie.entity.Project;
import com.kuiandjie.service.IdeaCommentService;
import com.kuiandjie.service.IdeaService;

public class IdeaServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		Project curp = (Project) session.getAttribute("curp");
		String type = request.getMethod();
		
		
		if(curp != null) {
			String id = request.getParameter("id");
			if("GET".equals(type) && id == null) {
				List<Idea> list = new IdeaService().findIdeaByProId(curp.getId());
				request.setAttribute("list", list);
				request.getRequestDispatcher("WEB-INF/views/idea.jsp").forward(request, response);
			} else if(!"".equals(id)) {
				int ideaId = Integer.parseInt(id);
				Idea idea = new IdeaService().findIdeaById(ideaId);
				List<IdeaComment> list = new IdeaCommentService().getCommentByIdeaId(ideaId);
				
				request.setAttribute("idea", idea);
				request.setAttribute("list", list);
				
				request.getRequestDispatcher("WEB-INF/views/showidea.jsp").forward(request, response);
			}
		} else {
			response.sendRedirect("main.jspx");
		}
	}
}
